package sd.czw.com.mmgapp.fragment;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;
import com.zhy.http.okhttp.OkHttpUtils;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.mcallback.MyGoodsDetatilsCallback;
import sd.czw.com.mmgapp.model.goodsbean.GoodsdetailleftModel;


/**
 * Created by czw
 * desc 描述：商品详情左边的fragment
 */
public class GoodsDetailFragmentLeft extends Fragment {


    @Bind(R.id.goods_banner)
    Banner goodsBanner;//头部图片
    @Bind(R.id.tv_title)
    TextView tvTitle;//商品标题
    @Bind(R.id.tv_sale)
    TextView tvSale;//本店价格
    @Bind(R.id.tv_shop)
    TextView tvShop;//市场价格
    @Bind(R.id.tv_kc)
    TextView tvKc;//商品库存
    @Bind(R.id.tvdjone)
    TextView tvdjone;//等级1 注册用户
    @Bind(R.id.tv_djtwo)
    TextView tvDjtwo;//等级2  银咩咩
    @Bind(R.id.tv_djthree)
    TextView tvDjthree;//等级3 皇咩咩
    @Bind(R.id.rltv)
    TextView rltv;
    @Bind(R.id.rl_gz)
    RelativeLayout rlGz;//退货规则
    private View view;
    private List<String> pictures = new ArrayList<>();

    public static GoodsDetailFragmentLeft newInstance(String goods_id) {
        Bundle args = new Bundle();
        args.putString("goods_id", goods_id);
        GoodsDetailFragmentLeft fragment = new GoodsDetailFragmentLeft();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_goodsdetailleft, container, false);
        //下载数据
        initdatas();
        //获取控件
        //initviews();
        ButterKnife.bind(this, view);
        return view;
    }

    //下载数据
    private void initdatas() {
        String goods_id = getArguments().getString("goods_id");
        System.out.println("goods_id:"+goods_id);
        OkHttpUtils.post()
                .url(Contants.GOODS_DETATIL)
                .addParams("time", Contants.Time).addParams("sign", Contants.Sign)
                .addParams("id", goods_id).build().execute(new MyGoodsDetatilsCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(GoodsdetailleftModel response, int id) {
                Integer errcode = response.getErrcode();
                GoodsdetailleftModel.ErrmsgBean errmsg = response.getErrmsg();
                if (errcode == 1 && errcode != null) {
                    //获取标题数据，并绑定tablayout+viewpager+fragment
                    setUpdatasToView(errmsg);
                } else if (errcode != null && errcode == 1001) {
                    Toast.makeText(getContext(), "请刷新重试", Toast.LENGTH_LONG).show();
                } else {
                    if (errmsg != null) {
                        Toast.makeText(getContext(), errmsg.toString(), Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
    }

    //将下载的数据在控件中显示
    private void setUpdatasToView(GoodsdetailleftModel.ErrmsgBean errmsg) {
        pictures.addAll(errmsg.getPictures());
        getPicturesBanner();
        tvTitle.setText(errmsg.getGoods().getGoods_name());
        tvSale.setText(errmsg.getGoods().getShop_price() + "元");
        tvShop.setText("市场价格："+errmsg.getGoods().getMarket_price());
        tvShop.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//设置中间线
        tvKc.setText("商品库存："+errmsg.getGoods().getGoods_number());
        for (int i = 0; i < errmsg.getRank_prices().size(); i++) {
            int select = Integer.parseInt(errmsg.getRank_prices().get(i).getSelect());
            switch (i) {
                case 0:
                    if (select == 1) {
                        tvdjone.setText(errmsg.getRank_prices().get(0).getRank_name() + ":" + errmsg.getRank_prices().get(0).getPrice() + "元(您的当前级别)");
                        tvdjone.setTextColor(Color.RED);
                    } else {
                        tvdjone.setText(errmsg.getRank_prices().get(0).getRank_name() + ":" + errmsg.getRank_prices().get(0).getPrice() + "元");
                    }
                    break;
                case 1:
                    if (select == 1) {
                        tvDjtwo.setText(errmsg.getRank_prices().get(1).getRank_name() + ":" + errmsg.getRank_prices().get(1).getPrice() + "元(您的当前级别)");
                        tvDjtwo.setTextColor(Color.RED);
                    } else {
                        tvDjtwo.setText(errmsg.getRank_prices().get(1).getRank_name() + ":" + errmsg.getRank_prices().get(1).getPrice() + "元");
                    }
                    break;
                case 2:
                    if (select == 1) {
                        tvDjthree.setText(errmsg.getRank_prices().get(2).getRank_name() + ":" + errmsg.getRank_prices().get(2).getPrice() + "元(您的当前级别)");
                        tvDjthree.setTextColor(Color.RED);
                    } else {
                        tvDjthree.setText(errmsg.getRank_prices().get(2).getRank_name() + ":" + errmsg.getRank_prices().get(2).getPrice() + "元");
                    }
                    break;
            }
        }
    }

    private void getPicturesBanner() {
        goodsBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                .setIndicatorGravity(BannerConfig.RIGHT)
                .setImages(pictures)
                .setDelayTime(4000)
                .setImageLoader(new GlideImageLoader())
                .start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            //下载图片
            Glide.with(getActivity().getApplicationContext()).load(path).into(imageView);
        }
    }
}
